package com.javastudy.forum.service.impl;

import com.javastudy.forum.common.AppResult;
import com.javastudy.forum.common.ResultCode;
import com.javastudy.forum.dao.ArticleMapper;
import com.javastudy.forum.exception.ApplicationException;
import com.javastudy.forum.model.Article;
import com.javastudy.forum.service.IArticleService;
import com.javastudy.forum.service.IBoardService;
import com.javastudy.forum.service.IUserService;
import com.javastudy.forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Chooker
 * @create 2023-08-17 23:00
 */
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
    @Resource
    ArticleMapper articleMapper;

    @Resource
    IUserService userService;

    @Resource
    IBoardService boardService;

    @Override
    public List<Article> selectAll() {
        List<Article> articles = articleMapper.selectAll();
        return articles;
    }

    @Override
    public List<Article> selectByBoardId(Long boardId) {
        if (boardId == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Article> articles = articleMapper.selectByBoardId(boardId);
        return articles;
    }

    @Override
    public void create(Article article) {
        if (article == null || article.getBoardId() == null || article.getUserId() == null ||
                StringUtils.isEmpty(article.getTitle()) || StringUtils.isEmpty(article.getContent())) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 设置默认值
        article.setVisitCount(0); // 访问数量
        article.setReplyCount(0); // 回复数量
        article.setLikeCount(0); // 点赞数量
        article.setState((byte) 0); // 状态
        article.setDeleteState((byte) 0); // 是否删除
        Date date = new Date();
        article.setCreateTime(date); // 创建时间
        article.setUpdateTime(date); // 更新时间

        int row = articleMapper.insertSelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_CREATE.toString() + " 新增帖子失败");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        userService.addOneArticleCountById(article.getUserId());

        boardService.addOneArticleCountById(article.getBoardId());
        // 打印日志   ---如何获取文章的id,可以在mapper.xml进行设置
        log.info(ResultCode.SUCCESS.toString() + " 帖子发布成功, articleId = " + article.getId());

    }

    @Override
    public Article selectById(Long id) {
        if (id == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectById(id);
        return article;
    }

    @Override
    public void updateVisitCountById(Long id) {
        if (id == null || id < 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        int row = articleMapper.updateVisitCountById(id);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public void modify(Long id, String title, String content) {
        if (id == null || id < 0 || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setId(id);

        article.setUpdateTime(new Date());  //设置更新时间

        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void delete(Long id) {
        if (id == null || id < 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = selectById(id);
        if (article == null || article.getUserId() == null || article.getBoardId() == null) {
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        article.setDeleteState((byte) 1);
        article.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        userService.subOneArticleCountById(article.getUserId());
        boardService.subOneArticleCountById(article.getBoardId());

        log.info("删除帖子成功, article id = " + article.getId() + ", user id = " + article.getUserId() + ".");

    }

    @Override
    public void thumbsUpById(Long id) {
        if (id == null || id < 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = selectById(id);
        if (article == null || article.getLikeCount() == null) {
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        article.setLikeCount(article.getLikeCount() + 1);
        article.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public void updateArticleCountById(Long id) {
        if (id == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = selectById(id);
        if (article == null) {
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }

        article.setReplyCount(article.getReplyCount() + 1);
        article.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        if (userId == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Article> articles = articleMapper.selectByUserId(userId);
        return articles;
    }


}
